/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.logger;

import ca.trader.utils.DebugUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Singleton logger for the Trader project
 * @author Ahsan
 */
public class TraderLogger
{

	 private static final Logger logger = Logger.getLogger(TraderLogger.class.getName());
	 private static FileHandler handlerFile;
	 private static LogFormatter formatterTxt;
	 private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	 // Used to initiate the logger
	 public static void init() throws IOException
	 {
		  // Log everything
		  logger.setLevel(Level.ALL);

		  // Set the log handler file.  The name of the handler file is "Trader-[Current Date]-[number of log files created today].log"
		  // Max size of a log is 100 MB.  After the max size is reached, a new log file is created.
		  handlerFile = new FileHandler("%h/Trader-" + getDateNow() + "-%g.log", 104857600, 10000, true);

		  // Create txt Formatter
		  formatterTxt = new LogFormatter();
		  handlerFile.setFormatter(formatterTxt);
		  logger.addHandler(handlerFile);



	 }

	 /**
	  * User to log a message at level INFO
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message  the message
	  */
	 public static void logInfo(String className, String methodName, String message)
	 {
		  logger.logp(Level.INFO, className, methodName, message);
	 }

	 /**
	  * User to log a message at level WARNING
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message  the message
	  */
	 public static void logWarning(String className, String methodName, String message)
	 {
		  logger.logp(Level.WARNING, className, methodName, message);
	 }

	 /**
	  * User to log a message at level SEVERE
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message  the message
	  * @param throwable the throwable that was thrown (the stack trace is printed as a string)
	  */
	 public static void logSevere(String className, String methodName, String message, Throwable throwable)
	 {

		  logSevere(className, methodName, message, DebugUtils.getStackTraceAsString(throwable));
	 }

	 /**
	  * User to log a message at level SEVERE
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message  the message
	  * @param stackTrace the stacktrace as a string
	  */
	 public static void logSevere(String className, String methodName, String message, String stackTrace)
	 {
		  logger.logp(Level.SEVERE, className, methodName, message + "\r\n" + stackTrace);
	 }

	 /**
	  * User to log a method entry
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * 
	  */
	 public static void logMethodEntry(String className, String methodName)
	 {
		  logger.entering(className, methodName);
	 }
	 
	 /**
	  * User to log a method exit
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * 
	  */
	 public static void logMethodExit(String className, String methodName)
	 {
		  logger.exiting(className, methodName);
	 }

	 /*
	  * Called when the logger should be closed (to flush the resources).
	  */
	 public static void exiting()
	 {
		  for (Handler h : logger.getHandlers())
		  {
			   h.close();
		  }
	 }

	 public static String getDateNow()
	 {
		  return dateFormat.format(new Date());
	 }

}